package com.fjrz.bbxwj.main.video

import android.app.Activity
import android.content.Intent
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.module.lemlin.mode.ViewModeActivity
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.mode.vm.VideoViewModel
import com.module.lemlin.base.loadCropImage
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.code
import com.module.lemlin.rxhttp.msg
import com.module.lemlin.view.DialogHintPopup
import com.module.lemlin.view.LoadingDialog
import kotlinx.android.synthetic.main.activity_video_advertising.*

class VideoAdvertisingActivity : ViewModeActivity<VideoViewModel>() {

    companion object {

        private const val EXTRA_KEY_VIDEO_ID = "EXTRA_KEY_VIDEO_ID"
        private const val EXTRA_KEY_AD_IMAGE = "EXTRA_KEY_AD_IMAGE"
        private const val EXTRA_KEY_AD_TITLE = "EXTRA_KEY_AD_TITLE"

        fun open(activity: Activity, videoId: Int, adImage: String, adTitle: String) {
            val intent = Intent(activity, VideoAdvertisingActivity::class.java)
            intent.putExtra(EXTRA_KEY_VIDEO_ID, videoId)
            intent.putExtra(EXTRA_KEY_AD_IMAGE, adImage)
            intent.putExtra(EXTRA_KEY_AD_TITLE, adTitle)
            activity.startActivity(intent)
        }
    }

    private val mVideoId: Int
        get() = intent.getIntExtra(EXTRA_KEY_VIDEO_ID, (0))

    private val mAdImage: String
        get() = intent.getStringExtra(EXTRA_KEY_AD_IMAGE) ?: ""

    private val mAdTitle: String
        get() = intent.getStringExtra(EXTRA_KEY_AD_TITLE) ?: ""

    private val hintPopup: DialogHintPopup by lazy {
        DialogHintPopup(context = this,
            onConfirm = {
                hintPopup.dismiss()
                finish()
            },
            onCancel = {

            })
    }

    override fun viewModel(): VideoViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_video_advertising

    override fun stateBar(): StatusBar =
        StatusBar(view = flVideoAdvertisingBar)

    override fun initView() {
        tvVideoAdvertisingTitle.text = mAdTitle

        ivVideoAdvertisingBack.setOnClickListener {
            finish()
        }

        tvVideoAdvertisingSignUp.setOnClickListener {
            viewModel.videoAdvertisingSignUp(etVideoAdvertisingNumber.text.toString(), mVideoId)
        }
        etVideoAdvertisingNumber.addTextChangedListener {
            tvVideoAdvertisingSignUp.isEnabled = !it.isNullOrEmpty()
        }
        ivVideoAdvertisingImage.loadCropImage(mAdImage)
    }

    override fun initViewMode() {
        viewModel.videoAdvertisingSignUpLiveData.observe(
            this as LifecycleOwner, Observer { response ->
                if (response.status == Status.START) {
                    LoadingDialog.show(context = this, resId = R.layout.dialog_load_style_1)
                } else if (response.status == Status.FAILURE) {
                    LoadingDialog.dismiss()
                    toast(response.throwable?.code, response.throwable?.msg)
                }
                if (response.status != Status.SUCCESS)
                    return@Observer
                LoadingDialog.dismiss()
                hintPopup.setTextTitle("提示")
                    .setTextMessage(response.httpData?.msg ?: "报名成功")
                    .setTextConfirm("好的")
                    .isTextCancelGone((true))
                    .setBackPressEnable(false)
                    .setOutSideDismiss(false)
                    .showPopupWindow()
            })
    }

}